package com.laicunba.util;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;

public class BigDecimalUtil {
	public static final MathContext MC = new MathContext(10, RoundingMode.HALF_DOWN);
	private final static DecimalFormat format = new DecimalFormat("0.00");
	private final static DecimalFormat format_Comman = new DecimalFormat("0,000.00");
	static {
		format.setRoundingMode(RoundingMode.FLOOR);
	}

	/**
	 * 第三位以后的小数直接省掉
	 * 
	 * @param bigDecimal
	 * @return
	 */
	public static String format(BigDecimal bigDecimal) {
		if (bigDecimal == null) {
			return null;
		}
		return format.format(bigDecimal.doubleValue());
	}

	public static BigDecimal parseWithComma(String commaAmt) {

		try {
			return BigDecimal.valueOf(Double.parseDouble(format_Comman.parseObject(commaAmt).toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String toCommanString(BigDecimal amt) {
		return format_Comman.format(amt.doubleValue());
	}

}
